package com.domain.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JOptionPane;

import com.domain.User;
import com.domain.impl.UserImpl;
import com.domain.service.UserService;
import com.domain.service.utils.ServiceUtils;
import com.views.CreateUserView;

public class CreateUserController {

	private CreateUserView createUserView;
	private ActionListener actionListener;

	public CreateUserController(CreateUserView createUserView) {

		this.createUserView = createUserView;
	}

	public void controll() {

		actionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Object obj = e.getSource();

				if (obj.equals(createUserView.getBtnSubmit())) {

					String userName = createUserView.getTxtUserName().getText();
					String password = new String(createUserView
							.getTxtPassword().getPassword());
					String question = createUserView.getTxtQuestion().getText();
					String answer = createUserView.getTxtAnswer().getText();

					if (userName.equals("") || password.equals("")
							|| question.equals("") || answer.equals("")) {

						createUserView.getLblMessages().setText(
								"Fields with * are mandatory");
						createUserView.getLblMessages().setVisible(true);

						if (userName.equals("")) {
							createUserView.getLblUserNameError().setVisible(
									true);
							createUserView.getTxtUserName().setBackground(
									Color.LIGHT_GRAY);
						} else {

							createUserView.getLblUserNameError().setVisible(
									false);
							createUserView.getTxtUserName().setBackground(
									Color.white);
						}
						if (password.equals("")) {
							createUserView.getLblPasswordError().setVisible(
									true);
							createUserView.getTxtPassword().setBackground(
									Color.LIGHT_GRAY);
						} else {
							createUserView.getLblPasswordError().setVisible(
									false);
							createUserView.getTxtPassword().setBackground(
									Color.white);
						}
						if (question.equals("")) {
							createUserView.getLblQuestionError().setVisible(
									true);
							createUserView.getTxtQuestion().setBackground(
									Color.LIGHT_GRAY);
						} else {
							createUserView.getLblQuestionError().setVisible(
									false);
							createUserView.getTxtQuestion().setBackground(
									Color.white);
						}
						if (answer.equals("")) {
							createUserView.getLblAnswerError().setVisible(true);
							createUserView.getTxtAnswer().setBackground(
									Color.LIGHT_GRAY);
						} else {
							createUserView.getLblAnswerError()
									.setVisible(false);
							createUserView.getTxtAnswer().setBackground(
									Color.white);
						}
						// JOptionPane.showMessageDialog(null,
						// "All details are mandatory", "Error Message",
						// JOptionPane.ERROR_MESSAGE);

					} else {

						User user = new UserImpl();

						user.setUserName(userName);
						user.setPassword(password);
						user.setQuestion(question);
						user.setAnswer(answer);

						UserService userService = ServiceUtils.getUserService();

						try {
							userService.create(user);
						} catch (IOException e1) {
							e1.printStackTrace();
						}

						createUserView.getLblMessages().setText(
								"Congrats!You have Successfully Registered");

						createUserView.getLblMessages().setVisible(true);
						createUserView.getLblUserNameError().setVisible(false);
						createUserView.getLblPasswordError().setVisible(false);
						createUserView.getLblQuestionError().setVisible(false);
						createUserView.getLblAnswerError().setVisible(false);
						createUserView.getTxtUserName().setBackground(
								Color.white);
						createUserView.getTxtPassword().setBackground(
								Color.white);
						createUserView.getTxtQuestion().setBackground(
								Color.white);
						createUserView.getTxtAnswer()
								.setBackground(Color.white);

						// createUserView.getLblSuccess().setVisible(true);
						// JOptionPane.showMessageDialog(null,
						// "You have Registered Successfully",
						// "Success Message",
						// JOptionPane.INFORMATION_MESSAGE);
					}

				}
				if (obj.equals(createUserView.getBtnReset())) {

					createUserView.getTxtUserName().setText("");
					createUserView.getTxtPassword().setText("");
					createUserView.getTxtQuestion().setText("");
					createUserView.getTxtAnswer().setText("");
					createUserView.getTxtUserName().requestFocus();

					createUserView.getLblMessages().setVisible(false);
					createUserView.getLblUserNameError().setVisible(false);
					createUserView.getLblPasswordError().setVisible(false);
					createUserView.getLblQuestionError().setVisible(false);
					createUserView.getLblAnswerError().setVisible(false);
					createUserView.getTxtUserName().setBackground(Color.white);
					createUserView.getTxtPassword().setBackground(Color.white);
					createUserView.getTxtQuestion().setBackground(Color.white);
					createUserView.getTxtAnswer().setBackground(Color.white);

				}

			}
		};

		createUserView.getBtnSubmit().addActionListener(actionListener);
		createUserView.getBtnReset().addActionListener(actionListener);

	}
}
